/**
 * 载入项目引导文件
 */
require('./bootstrap');

let box;

$(function () {
    let tableOptions = utils.bootstraptableOptions;
    //请求后台的URL
    tableOptions.url = page_params.url_list;
    //搜索参数
    tableOptions.queryParams = function (params) {//传递参数（*）
        //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
        var temp = {
            pageSize:params.pageSize,
            page:params.pageNumber,
            keyword:$("#keyword").val(),
        };
        return temp;
    };
    tableOptions.columns = [
        {field: 'mass_id',title: '群发ID'},
        {field: 'msg_id',title: '消息发送任务ID'},
        {field: 'title',title: '名称'},
        {field: 'msg_type_str',title: '群发消息类型',},
        {
            field: 'is_open',title: '是否开启',
            formatter:function (value, row, index) {
                if (value == 1){
                    return '<span class="label label-info">开启</span>';
                }else{
                    return '<span class="label label-warning">关闭</span>';
                }
            }
        },
        {field: 'push_time',title: '群发时间'},
        {field: 'errcode',title: '群发状态码'},
        {field: 'errmsg',title: '群发结果'},
        {
            field: 'operate',
            title: '操作',
            formatter: function (value, row, index) {
                let buttons = [];
                if (_.isEmpty(row.msg_id) && page_params.url_edit){
                    buttons.push('<a class="btn btn-primary btn-sm btn-edit" href="'+ page_params.url_edit +'?id='+ row.mass_id +'"><i class="fa fa-pencil"></i> 编辑</a>');
                }
                if (row.msg_id == 0 && page_params.url_preview){
                    buttons.push('<button class="btn btn-primary btn-sm btn-preview"><i class="fa fa-eye"></i> 预览</button>');
                }
                if (row.msg_id != 0 && page_params.url_syncstatus){
                    buttons.push('<button class="btn btn-primary btn-sm btn-syncstatus"><i class="fa fa-refresh"></i> 刷新</button>');
                }
                if (page_params.url_del){
                    buttons.push('<button class="btn btn-warning btn-sm btn-del"><i class="fa fa-trash"></i> 删除</button>');
                }
                return buttons.join("\r\n");
            },//自定义方法，添加操作按钮
            events : {
                'click .btn-preview' : function (e, value, row, index) {
                    $("#box-preview form").resetForm();
                    $('#box-preview form').attr('action', page_params.url_preview);
                    $("#box-preview input[name='mass_id']").val(row.mass_id);
                    $("#box-preview input[name='msg_type']").val(row.msg_type);
                    $("#box-preview input[name='openid']").val('');
                    box = layer.open({
                        type: 1,
                        title: '群发预览',
                        anim: 2,
                        shadeClose: true, //开启遮罩关闭
                        skin: 'layui-layer-molv', //加上边框
                        area: ['500px', '250px'], //宽高
                        content: $('#box-preview')
                    });
                },
                'click .btn-syncstatus' : function (e, value, row, index) {
                    utils.ajaxPost(page_params.url_syncstatus,{'mass_id':row.mass_id},(response, postdata) => {
                        data = response.data;
                        utils.showToastr(data);
                        if (data.status == 'success') {
                            layer.close(box);
                            table.bootstrapTable('refresh');
                        }
                    });
                },
                'click .btn-del': function (e, value, row, index) {
                    utils.confirm(page_params.url_del,{'mass_id':row.mass_id}, (response, data) =>{
                        data = response.data;
                        utils.showToastr(data);
                        if (data.status == 'success') {
                            table.bootstrapTable('refresh');
                        }
                    });
                }
            }
        },
    ];
    let table = $("#table").bootstrapTable(tableOptions);
    //搜索按钮
    $("#btn-search").on('click', function () {
        table.bootstrapTable('refresh');
    });
    //提交预览
    $("#box-preview .btn-sub").on('click', function () {
        utils.ajaxSubmitForm($("#box-preview form"), '', {}, (response, postdata) => {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(box);
                table.bootstrapTable('refresh');
            }
        });
    });
    //群发速度设置
    $("#btn-speed").on('click', function () {
        utils.ajaxGet(page_params.url_upsetspeed, {}, (response, postdata) => {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                $("#box-speed form").resetForm();
                $('#box-speed form').attr('action', page_params.url_upsetspeed);
                $("#box-speed select[name='speed']").val(data.data.speed);
                box = layer.open({
                    type: 1,
                    title: '群发速度设置',
                    anim: 2,
                    shadeClose: true, //开启遮罩关闭
                    skin: 'layui-layer-molv', //加上边框
                    area: ['500px', '200px'], //宽高
                    content: $('#box-speed')
                });
            }
        });
    });
    //保存群发速度
    $("#box-speed .btn-sub").on('click', function () {
        utils.ajaxSubmitForm($("#box-speed form"), '', {}, (response, postdata) => {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(box);
            }
        });
    });
});